package data;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JOptionPane;

import miscellaneous.Storage;
import data.room.Reservation;


/**
 * Contains information about an event.
 * @author Erik
 *
 */

public class Appointment extends Data implements PropertyChangeListener {
	
	private Reservation reservation;
	private String title;						//the title of this appointment
	private String description;					//the description of this appointment
	private String location;					//description of this appointment's location
	private User owner;
	
	/**
	 * Initializes the fields of the appointment.
	 * @param endTime The end time of the appointment
	 * @param description A description of the appointment
	 * @param location A description of the appointment location
	 * @param owner The user this appointment concerns
	 * @param reservation The reservation of the appointment
	 */
	public Appointment(String title, String description, String location, 
			User owner, Reservation reservation) {
		super();
		if(reservation == null ) {
			this.reservation = new Reservation(null, null);
		} else {
			this.reservation = reservation;
		}
		
		this.reservation.addPropertyChangeListener(this);
		this.reservation.setAppointment(this);
		
		this.title = title;
		this.description = description;
		this.location = location;
		this.owner = owner;
		
	}

	/**
	 * Updates all the attributes and then fire a propertyChangeSupport (only once).
	 * Every change must go through this method.
	 * @param title The title to the appointment
	 * @param description The description to the appointment
	 * @param location The location to the appointment
	 * @param reservation The reservation to the appointment
	 */
	public void UpdateAll(String title, String description, String location, 
			Reservation reservation) {
		if(getOwner() != Storage.getloggedInUser()) {
			JOptionPane.showMessageDialog(null, "Kun eier/møteleder kan endre dette møtet");
		} else {
			setTitle(title);
			setDescription(description);
			setLocation(location);
			setReservation(reservation);

			changeSupport.firePropertyChange("update", null, null);
		}
	}
	
	public String getTitle() {
		return title;
	}

	private void setTitle(String title) {
		this.title = title;
	}

	public Reservation getReservation() {
		return reservation;
	}
	
	public void setReservation(Reservation reservation) {
		if(this.reservation != null){
			this.reservation.removePropertyChangeListener(this);
		}
		this.reservation = reservation;
		reservation.setAppointment(this);
		if(reservation != null){
			this.reservation.addPropertyChangeListener(this);
		}
	}
	
	public String getDescription() {
		return description;
	}
	
	private void setDescription(String description) {
		this.description = description;
	}
	
	public String getLocation() {
		return location;
	}
	
	private void setLocation(String location) {
		this.location = location;
	}

	public User getOwner() {
		return owner;
	}
	
	public void setOwner(User owner) {
		this.owner = owner;
	}
	
	/**
	 * Run this method when the appointment should be edited
	 */
	public void requestEdit() {
		changeSupport.firePropertyChange("edit", null, null);
	}

	@Override
	public int compareTo(Data d) {
		if(! (d instanceof Appointment)) {
			System.err.println("Comparing appointment to some other class");
			return 0;
		}
		
		Appointment a = (Appointment) d;
		
		if(getReservation() != null && a != null &&
				a.getReservation() != null) {
			
			if(getReservation().getStartTime() != null && 
					a.getReservation().getStartTime() != null) {
				
				return (int)(getReservation().getStartTime().getTimeInMillis() - 
						a.getReservation().getStartTime().getTimeInMillis());
			}
		}
				
		return Integer.MIN_VALUE;
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		changeSupport.firePropertyChange("update", null, null);
	}

}
